package com.sunlake.spring.main.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 * RabbitMQ配置类
 * 定义交换机、队列、绑定关系
 * @author by liuhanzhi
 */
@Configuration
public class RabbitMQConfig {
    // 定义交换机名称
    public static final String BUSINESS_EXCHANGE = "business.exchange";
    
    // 定义队列名称
    public static final String PAY_QUEUE = "pay.queue";
    public static final String ORDER_QUEUE = "order.queue";
    public static final String LOG_QUEUE = "log.queue";
    
    // 定义routing key
    public static final String PAY_ROUTING_KEY = "pay";
    public static final String ORDER_ROUTING_KEY = "order";
    public static final String LOG_ROUTING_KEY = "log";
    
    // 消息转换器，使用Jackson2JsonMessageConverter处理JSON消息
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    
    // 配置RabbitTemplate，显式设置消息转换器
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }
    
    // 配置监听器容器工厂，确保监听也使用相同的消息转换器
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        return factory;
    }
    
    // 声明Direct交换机
    @Bean
    public DirectExchange businessExchange() {
        return new DirectExchange(BUSINESS_EXCHANGE);
    }
    
    // 声明队列
    @Bean
    public Queue payQueue() {
        return new Queue(PAY_QUEUE, true);
    }
    
    @Bean
    public Queue orderQueue() {
        return new Queue(ORDER_QUEUE, true);
    }
    
    @Bean
    public Queue logQueue() {
        return new Queue(LOG_QUEUE, true);
    }
    
    // 绑定队列到交换机，通过routing key关联
    @Bean
    public Binding payBinding(Queue payQueue, DirectExchange businessExchange) {
        return BindingBuilder.bind(payQueue).to(businessExchange).with(PAY_ROUTING_KEY);
    }
    
    @Bean
    public Binding orderBinding(Queue orderQueue, DirectExchange businessExchange) {
        return BindingBuilder.bind(orderQueue).to(businessExchange).with(ORDER_ROUTING_KEY);
    }
    
    @Bean
    public Binding logBinding(Queue logQueue, DirectExchange businessExchange) {
        return BindingBuilder.bind(logQueue).to(businessExchange).with(LOG_ROUTING_KEY);
    }
}